/**
 * Created by JetBrains WebStorm.
 * User: hp-stuff
 * Date: 13-4-20
 * Time: 11:33
 * To change this template use File | Settings | File Templates.
 */
DB = (function () {

	function DB() {
		this.db = null;
	}

	DB.getInstance = function(){
		if(this._instance){
			return this._instance;
		}
		return this._instance = new DB();
	};

	DB.prototype.setOnOpenListener = function(listener){
		this.openListener = listener;
	};

	DB.prototype.setOnUpgradeListener = function(listener){
		this.upgradeListener = listener;
	};

	DB.prototype.setOnErrorListener = function(listener){
		this.errorListener = listener;
	};

	DB.prototype.open = function(name, version){
		var request, _this = this;
		this.version = version || 0;
		this.name = name;
		request = indexedDB.open(this.name, this.version);
		request.onupgradeneeded = function(e) {
			var db = e.target.result;
			if(_this.upgradeListener) _this.upgradeListener.call(_this, e);
		};
		request.onsuccess = function(e){
			_this.db = e.target.result;
			if(_this.openListener) _this.openListener.call(_this, e);
		};
		request.onerror = function(e){
			if(_this.errorListener) _this.errorListener.call(_this, e);
		}
	};

	DB.prototype.addItem = function(store, item){
		var trans = this.db.transaction([store], "readwrite");
		var store = trans.objectStore(store);
		var request = store.put(item);
	};

	DB.prototype.removeItem = function(id){
		var trans = this.db.transaction([store], "readwrite");
		var store = trans.objectStore(store);
		var request = store.delete(id);
	};

	DB.prototype.getStore = function(store){

	};

	return DB;

})();